﻿using ExtendNetease_DGJModule.MixMusicApi.M;
using Newtonsoft.Json.Linq;
using RestSharp;
using RestSharp.Extensions;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ExtendNetease_DGJModule.MixMusicApi
{
    public class API
    {
        public static int Compute(string s, string t)
        {
            if (string.IsNullOrEmpty(s))
            {
                if (string.IsNullOrEmpty(t))
                    return 0;
                return t.Length;
            }

            if (string.IsNullOrEmpty(t))
            {
                return s.Length;
            }

            int n = s.Length;
            int m = t.Length;
            int[,] d = new int[n + 1, m + 1];

            // initialize the top and right of the table to 0, 1, 2, ...
            for (int i = 0; i <= n; d[i, 0] = i++) ;
            for (int j = 1; j <= m; d[0, j] = j++) ;

            for (int i = 1; i <= n; i++)
            {
                for (int j = 1; j <= m; j++)
                {
                    int cost = (t[j - 1] == s[i - 1]) ? 0 : 1;
                    int min1 = d[i - 1, j] + 1;
                    int min2 = d[i, j - 1] + 1;
                    int min3 = d[i - 1, j - 1] + cost;
                    d[i, j] = Math.Min(Math.Min(min1, min2), min3);
                }
            }
            return d[n, m];
        }
        public static TOPSongInfo TOPSearch(string Keyword)
        {
            var client = new RestClient("https://pthome-api.duan.red/WBMusic/Find");
            client.Timeout = 2000;
            var request = new RestRequest(Method.GET);
            request.AlwaysMultipartFormData = true;
            request.AddParameter("Key", Keyword.Trim());
            IRestResponse response = client.Execute(request);
            var v = response.Content;
            if (v == "null")
            {
                return null;
            }
            else
            {
                var m= Newtonsoft.Json.JsonConvert.DeserializeObject<TOPSongInfo>(v);
                if (m!=null)
                {
                    m.Id = -(int.MaxValue - m.Id);
                }
                m.Id = DateTimeOffset.Now.ToUnixTimeMilliseconds();
                return m;
            }
        }
        /// <summary>
        /// 搜索
        /// </summary>
        /// <param name="keyword"></param>
        /// <param name="p"></param>
        /// <returns></returns>
        public static string Search(string keyword,string p="163")
        {
            try
            {
                var url = string.Format("http://y.jsososo.com/apiMix/search?key={0}&pageNo=1&type=0&_p={1}", keyword, p);
                var client = new RestClient(url);
                client.Timeout = 5000;
                var request = new RestRequest(Method.GET);
                IRestResponse response = client.Execute(request);
                if (response.IsSuccessful)
                {
                    return response.Content;
                }
                return null;
            }
            catch (Exception ex)
            {
                return null;
            }
            
        }
        /// <summary>
        /// 查找歌词
        /// </summary>
        /// <param name="id"></param>
        /// <param name="p"></param>
        /// <returns></returns>
        public static string Lyric(string id, string p)
        {
            try
            {
                var ts = DateTimeOffset.Now.ToUnixTimeMilliseconds();
                var url = string.Format("http://y.jsososo.com/apiMix/lyric?id={0}&_p={1}&_t={2}", id, p, ts);
                var client = new RestClient(url);
                client.Timeout = 5000;
                var request = new RestRequest(Method.GET);
                IRestResponse response = client.Execute(request);
                if (response.IsSuccessful)
                {
                    return response.Content;
                }
                return null;
            }
            catch (Exception ex)
            {
                return null;
            }
        }
        public static string GetDownLoadUrl(SongInfoMix songMix)
        {
            try
            {
                var ts = DateTimeOffset.Now.ToUnixTimeMilliseconds();
                var dwjson = "{\r\n    \"list\": [\r\n        {\r\n           " +
                                  " \"key\": \"" + songMix.name + "\",\r\n " +
                                  "           \"id\": \"" + songMix.aId + "\",\r\n            " +
                                  "\"duration\": " + songMix.duration + "\r\n        }\r\n    ],\r\n  " +
                                  "  \"_p\": \"" + songMix.platform + "\",\r\n    " +
                                  "\"_t\": " + ts + "\r\n}";
                var client = new RestClient("http://y.jsososo.com/apiMix/song/find");
                client.Timeout  = 5000;
                var request = new RestRequest(Method.POST);
                //request.AddHeader("Host", " y.jsososo.com");
                //request.AddHeader("Host-Check", " MjAyMTAxMTc=");
                request.AddHeader("Origin", " http://y.jsososo.com");
                request.AddHeader("Referer", " http://y.jsososo.com/");
                client.UserAgent = " Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.141 Safari/537.36 Edg/87.0.664.75";
                request.AddHeader("Content-Type", "application/json");
                request.AddParameter("application/json", dwjson, ParameterType.RequestBody);
                IRestResponse response = client.Execute(request);
                if (response.IsSuccessful)
                {
                    return response.Content;
                }
                return null;
            }
            catch (Exception ex)
            {

                return null;
            }
            

        }
        public static void DownLoad(string url,string savepath)
        {
            var request = new RestRequest(url, Method.GET);
            var client = new RestClient();
            client.DownloadData(request).SaveAs(savepath);
        }
    }
}
